﻿using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Client;
using Raven.Client.Document;
using LogManager.Base;
using LogManager.Base.Data;

namespace LogManager.LocalStorage.Data
{
    public class UserSettingsService : IUserSettingsService
    {
        private IDocumentStore _store;

        public UserSettingsService()
        {
            _store = new DocumentStore()
                         {
                             Url = "http://localhost:9000"
                         };
        }

        public void Dispose()
        {
            _store.Dispose();
            GC.SuppressFinalize(this);
        }

        public IQueryable<IUserSettings> Query()
        {
            using (var session = _store.OpenSession())
            {
                return session.Query<UserSettings>();
            }
        }

        public IEnumerable<IUserSettings> All()
        {
            using (var session = _store.OpenSession())
            {
                return session.Load<UserSettings>();
            }
        }

        public IUserSettings Get(string userId)
        {
            using (var session = _store.OpenSession())
            {
                return session.Load<UserSettings>(userId);
            }
        }

        public void Save(UserSettings userSettings)
        {
            using (var session = _store.OpenSession())
            {
                session.Store(userSettings);
                session.SaveChanges();
            }
        }
    }
}
